package bt15012014_phanso;

import java.util.Scanner;

public class PhanSo {
private int tu;
private int mau;

//Phuong thuc khoi tao ko tham so
public PhanSo(){
	
}

//Phuong thuc khoi tao co 2 tham so
public PhanSo(int tu, int mau){
	this.tu = tu;
	this.mau = mau;
}

//Phuong thuc nhap
public void nhap(){
	Scanner str = new Scanner(System.in);
	
	System.out.println("Vui long nhap tu so: ");
	this.tu = str.nextInt();
	System.out.println("Vui long nhap mau so: ");
	this.mau = str.nextInt();
}

//Phuong thuc cong hai phan so
public PhanSo congPhanSo(PhanSo p){
	int xTu = this.tu*p.mau + p.tu*this.mau;
	int xMau = this.mau*p.mau;
	
	return new PhanSo(xTu,xMau).toiGian();
}

//Phuong thuc tru hai phan so
public PhanSo truPhanSo(PhanSo p){
	int xTu = this.tu*p.mau - p.tu*this.mau;
	int xMau = this.mau*p.mau;
	
	return new PhanSo(xTu,xMau).toiGian();
}

//Phuong thuc nhan hai phan so
public PhanSo nhanPhanSo(PhanSo p){
	int xTu = this.tu*p.tu;
	int xMau = this.mau*p.mau;
	
	return new PhanSo(xTu,xMau).toiGian();
}

//Phuong thuc chia hai phan so
public PhanSo chiaPhanSo (PhanSo p){
	int xTu = this.tu*p.mau;
	int xMau = this.mau*p.tu;
	
	return new PhanSo (xTu,xMau).toiGian();
}

//Phuong thuc tim uoc chung lon nhat
private int timUCLN (){
	int xTu = Math.abs(this.tu);
	int xMau = Math.abs(this.mau);
	
	if (xTu==0)
		return xTu+xMau;
	while (xTu!=xMau)
	{
		if (xTu>xMau)
			xTu = xTu-xMau;
		else
			xMau = xMau - xTu;
	}
	return xTu;
}

//Puong thuc toi gian phan so
public PhanSo toiGian(){
	int uoc = this.timUCLN();
	int xTu = this.tu/uoc;
	int xMau = this.mau/uoc;
	
	return new PhanSo(xTu,xMau);
}

//Phuong thuc so sanh hai phan so
public boolean soSanh(PhanSo p1){
	PhanSo a = this.toiGian();
	PhanSo b = p1.toiGian();
	
	if (a.tu == b.tu && a.mau == b.mau){
		return true;
	}
	return false;
}

//Chuyen ve string
public String toString(){
	return this.tu + "/" + this.mau;
}
}
